<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--仓库查询数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="供应商编号" prop="supplierCode" label-width="90px">
            <el-input
              v-model="queryParams.supplierCode"
              placeholder="请输入供应商编号"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="供应商名称" prop="supplierName" label-width="90px">
            <el-input
              v-model="queryParams.supplierName"
              placeholder="请输入供应商名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="类型" prop="supplierType">
            <el-select
              v-model="queryParams.supplierType"
              placeholder="类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.wms_supplier_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="电话" prop="userTel">
            <el-input
              v-model="queryParams.userTel"
              placeholder="请输入电话"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['wms:supplier:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['wms:supplier:export']"
            >导出</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="dataList" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="供应商编号" align="center" key="supplierCode" prop="supplierCode" />
          <el-table-column label="供应商名称" align="center" key="supplierName" prop="supplierName"/>
          <el-table-column label="类型" align="center" key="supplierType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_supplier_type" :value="scope.row.supplierType"/>
            </template>
          </el-table-column>
          <el-table-column label="电话" align="center" key="userTel" prop="userTel"/>
          <el-table-column label="传真" align="center" key="fax" prop="fax"/>
          <el-table-column label="Email" align="center" key="email" prop="email"/>
          <el-table-column label="联系人" align="center" key="userName" prop="userName"/>
          <el-table-column label="地址" align="center" key="address" prop="address"/>
          <el-table-column label="创建时间" align="center" prop="createDate" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="260"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="showContact(scope.row)"
                v-hasPermi="['wms:supplier:contact']"
              >联系人设置</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['wms:supplier:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['wms:supplier:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商编号" prop="supplierCode">
              <el-input v-model="form.supplierCode" placeholder="请输入供应商编号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商类型" prop="supplierType">
              <el-select v-model="form.supplierType" placeholder="请选择供应商类型" clearable>
                <el-option
                  v-for="dict in dict.type.wms_supplier_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入联系人" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="userTel">
              <el-input v-model="form.userTel" placeholder="请输入电话" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址"  maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="'联系人'" :visible.sync="contactOpen" width="900px" :close-on-click-modal="false" :close-on-press-escape="false" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="addContact"
            :disabled="addFlag"
            v-hasPermi="['wms:supplier:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="exportContact"
            v-hasPermi="['wms:supplier:export']"
          >导出</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="contactLoad" :data="contactList">
        <el-table-column label="联系人" align="center" key="userName">
          <template slot-scope="scope">
            <el-input v-model="scope.row.userName" clear v-show="scope.$index === cellIndex"></el-input>
            <p v-show="scope.$index !== cellIndex">{{scope.row.userName}}</p>
          </template>
        </el-table-column>
        <el-table-column label="地址" align="center" key="address">
          <template slot-scope="scope">
            <el-input v-model="scope.row.address" clear v-show="scope.$index === cellIndex"></el-input>
            <p v-show="scope.$index !== cellIndex">{{scope.row.address}}</p>
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center" key="userTel">
          <template slot-scope="scope">
            <el-input v-model="scope.row.userTel" clear v-show="scope.$index === cellIndex"></el-input>
            <p v-show="scope.$index !== cellIndex">{{scope.row.userTel}}</p>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createDate" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="editContact(scope)"
              v-hasPermi="['wms:supplier:edit']"
              v-show="scope.$index !== cellIndex"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="saveContact(scope)"
              v-hasPermi="['wms:supplier:edit']"
              v-show="scope.$index === cellIndex"
            >保存</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="delContact(scope)"
              v-hasPermi="['wms:supplier:remove']"
              v-show="scope.$index !== cellIndex"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="contactOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addObj, delObj, editObj, findAllList, findById } from "@/api/wms/supplier";
import { checkEmail,checkPhone }from "@/utils/ruoyi"
import {addObj as addContact, delObj as delContact, editObj as editContact, getContactList} from "@/api/wms/contact";

export default {
  name: "SupplierIndex",
  dicts: ['wms_supplier_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据列表
      dataList: [],
      // 日期范围
      dateRange: [],
      // 联系人弹窗
      contactOpen: false,
      contactLoad: false,
      contactList: [],
      cellIndex: null,
      contactForm: {},
      selectId: undefined,
      addFlag: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierCode: undefined,
        supplierName: undefined,
        supplierType: undefined,
        userTel: undefined
      },
      // 表单校验
      rules: {
        supplierCode: [
          { required: true, message: "供应商编号不能为空", trigger: "blur" },
          { required: true, message: "供应商编号不能为空", trigger: "change" }
        ],
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" },
          { min: 2, max: 50, message: '供应商名称必须介于 2 和 50 之间', trigger: 'blur' }
        ],
        supplierType: [
          { required: true, message: "供应商类型不能为空", trigger: ['change'] },
        ],
        userTel: [
          checkPhone()
        ],
        email: [
          checkEmail(),
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addContact(){
      this.contactList.push({
        userName: undefined,
        userTel: undefined,
        address: undefined,
        createDate: undefined
      })
      this.addFlag = true
      this.cellIndex = this.contactList.length-1
    },
    editContact({$index, row}){
      this.cellIndex = $index
    },
    saveContact({$index, row}){
      row.sourceId = this.selectId
      if (row.id !== undefined) {
        editContact(row).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.cellIndex = null
          this.addFlag = false
        });
      } else {
        addContact(row).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cellIndex = null
          row.id = response.data.id
          row.createDate = response.data.createDate
          this.addFlag = false
        });
      }
    },
    delContact({$index, row}){
      this.$modal.confirm('是否确认删除联系人为"' + row.userName + '"的数据项？').then(function() {
        return delContact(row.id);
      }).then(() => {
        this.findContactList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    exportContact(){
      this.download('wmsContactInfo/export', {sourceId: this.selectId}, `contact_${new Date().getTime()}.xlsx`)
    },
    showContact(row){
      this.selectId = row.id
      this.findContactList()
    },
    findContactList(){
      this.contactOpen = true
      this.contactLoad = true
      getContactList({sourceId: this.selectId}).then(response => {
          this.contactList = response.data;
          this.contactLoad = false;
        }
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wmsSupplierInfo/export', {
        ...this.queryParams
      }, `supplier_${new Date().getTime()}.xlsx`)
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      findAllList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        supplierCode: undefined,
        supplierName: undefined,
        supplierType: undefined,
        userName: undefined,
        userTel: undefined,
        fax: undefined,
        address: undefined,
        email: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      findById(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            editObj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addObj(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除供应商为"' + row.supplierCode + '"的数据项？').then(function() {
        return delObj(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
